package com.yanqu.road.server.manger.cross3;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.entity.enums.rank.eRankRangeType;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankMap;
import com.yanqu.road.logic.xlogic.rank.season.XSeasonUserRankMap;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankMap;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import java.util.List;

public class GameXRankMgr extends TempMgr {

    // 跨地区 个人榜
    private static XUserRankMap userRankMap = new XUserRankMap(DbNameStrategy.getUserDb(), "t_u_xrank_user",
            ChannelActivityMgr::getChannelActivityMaxRank);

    // 跨地区 区服榜
    private static XChannelRankMap channelRankMap = new XChannelRankMap(DbNameStrategy.getUserDb(), "t_u_xrank_channel_part",
            ChannelActivityMgr::getChannelActivityMaxRank,
            ChannelActivityMgr::getChannelRankPartNum);

    // 跨地区 赛季个人榜
    private static XSeasonUserRankMap seasonUserRankMap = new XSeasonUserRankMap(DbNameStrategy.getUserDb(), "t_u_xrank_season_user",
            SeasonHelper::getChannelSeasonMaxRank);

    public static XUserRankMap getUserRankMap() {
        return userRankMap;
    }

    public static XChannelRankMap getChannelRankMap() {
        return channelRankMap;
    }

    public static XSeasonUserRankMap getSeasonUserRankMap() {
        return seasonUserRankMap;
    }

    private static void loadShowTimeXRank() {
        // 把正在进行的跨域的活动读进来
        for (ActivityInfo activityInfo : NormalActivityMgr.getShowTimeChannelActivityInfo()) {
            List<eRankInfo> rankInfoList = eRankInfo.getERankInfoList(activityInfo.getType(), activityInfo.getChildType());
            for (eRankInfo rankInfo : rankInfoList) {
                if (rankInfo.getRangeType() == eRankRangeType.User) {
                    getUserRankMap().getUserRankListModel(activityInfo.getActivityId(), rankInfo.getConditionType());
                } else if (rankInfo.getRangeType() == eRankRangeType.Channel) {
                    getChannelRankMap().getChannelRankListModel(activityInfo.getActivityId(), rankInfo.getConditionType());
                }
            }
        }
    }

    @Override
    public boolean save() {
        userRankMap.save();
        channelRankMap.save();
        seasonUserRankMap.save();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        loadShowTimeXRank();
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
}
